parent nodes: PrefuseDoku
Hello World
This is just a simple Hello World example wich shows the basic concepts.
The most exiting features such as animations are not included here.
import javax.swing.JFrame;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.assignment.ColorAction;
import prefuse.action.layout.graph.NodeLinkTreeLayout;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.LabelRenderer;
import prefuse.util.ColorLib;
import prefuse.visual.VisualItem;
public class HelloWorldPrefuse extends Display {
private static final String LABEL = "label";
Graph graph;
public static final String GRAPH = "graph";
public static final String NODES = "graph.nodes";
public static final String EDGES = "graph.edges";
public HelloWorldPrefuse() {
// setup a visualisation
super(new Visualization());
// generate a graph
initGraph();
// standard labelRenderer for the given label
LabelRenderer nodeRenderer = new LabelRenderer(LABEL);
// rendererFactory for the visualization items
DefaultRendererFactory rendererFactory = new DefaultRendererFactory();
// set the labelRenderer
rendererFactory.setDefaultRenderer(nodeRenderer);
m_vis.setRendererFactory(rendererFactory);
// Color Actions
ColorAction nodeText = new ColorAction(NODES, VisualItem.TEXTCOLOR);
nodeText.setDefaultColor(ColorLib.gray(0));
ColorAction nodeStroke = new ColorAction(NODES, VisualItem.STROKECOLOR);
nodeStroke.setDefaultColor(ColorLib.gray(100));
ColorAction nodeFill = new ColorAction(NODES, VisualItem.FILLCOLOR);
nodeFill.setDefaultColor(ColorLib.gray(255));
ColorAction edgeStrokes = new ColorAction(EDGES, VisualItem.STROKECOLOR);
edgeStrokes.setDefaultColor(ColorLib.gray(100));
// bundle the color actions
ActionList draw = new ActionList();
draw.add(nodeText);
draw.add(nodeStroke);
draw.add(nodeFill);
draw.add(edgeStrokes);
m_vis.putAction("draw", draw);
// create the layout action for the graph
NodeLinkTreeLayout treeLayout = new NodeLinkTreeLayout(GRAPH);
m_vis.putAction("treeLayout", treeLayout);
// run actions
m_vis.run("treeLayout");
m_vis.run("draw");
}
private void initGraph() {
graph = new Graph();
// first define a column
graph.addColumn(LABEL, String.class);
// then add nodes
Node h = graph.addNode();
h.setString(LABEL, "h");
Node e = graph.addNode();
e.setString(LABEL, "e");
Node l1 = graph.addNode();
l1.setString(LABEL, "l");
Node l2 = graph.addNode();
l2.setString(LABEL, "l");
Node o1 = graph.addNode();
o1.setString(LABEL, "o");
Node blank = graph.addNode();
blank.setString(LABEL, " ");
Node w = graph.addNode();
w.setString(LABEL, "w");
Node o2 = graph.addNode();
o2.setString(LABEL, "o");
Node r = graph.addNode();
r.setString(LABEL, "r");
Node l3 = graph.addNode();
l3.setString(LABEL, "l");
Node d = graph.addNode();
d.setString(LABEL, "d");
// then add the edges
graph.addEdge(h, e);
graph.addEdge(e, l1);
graph.addEdge(l1, l2);
graph.addEdge(l2, o1);
graph.addEdge(o1, blank);
graph.addEdge(blank, w);
graph.addEdge(w, o2);
graph.addEdge(o2, r);
graph.addEdge(r, l3);
graph.addEdge(l3, d);
m_vis.addGraph(GRAPH, graph);
}
public static void main(String[] args) {
HelloWorldPrefuse hello = new HelloWorldPrefuse();
JFrame frame = new JFrame("Hello World");
frame.getContentPane().add(hello);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,400);
frame.setVisible(true);
}
}